package com.zc.controller;

import com.github.pagehelper.PageInfo;
import com.zc.annotation.SystemLogAnnotation;
import com.zc.dto.SysRoleDto;
import com.zc.pojo.SysRole;
import com.zc.service.SysResourceService;
import com.zc.service.SysRoleService;
import com.zc.utils.JSONResult;
import com.zc.dto.TreeSysResourceDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/sysRole")
public class SysRoleController {

    @Autowired
    private SysRoleService sysRoleService;

    @Autowired
    private SysResourceService sysResourceService;




    @SystemLogAnnotation("角色信息跳转列表页面")
    @GetMapping("toList")
    public String toList() {
        return "sysRole/sysRoleList";
    }

    @SystemLogAnnotation("角色信息列表查询")
    @GetMapping("list")
    @ResponseBody
    public JSONResult list(String roleName, Integer page, Integer limit) {
        Map<String, Object> data = new HashMap<>();
        PageInfo<SysRole> pageInfo = sysRoleService.queryByPage(roleName, page, limit);

        List<SysRole> sysRoleList = pageInfo.getList();

        data.put("sysRoleSize", pageInfo.getTotal());
        data.put("sysRoles", sysRoleList);
        return JSONResult.ok(data);
    }

    @SystemLogAnnotation("角色信息跳转新增页面")
    @GetMapping("toAdd")
    public String toAdd(Model model) {

        List<SysRole> sysRoleList = sysRoleService.queryAll();
        model.addAttribute("sysRoleList", sysRoleList);

        return "sysRole/sysRoleAdd";
    }

    @SystemLogAnnotation("角色信息新增")
    @PostMapping("add")
    @ResponseBody
    public JSONResult Add(@RequestBody SysRoleDto sysRoleDto) {

        sysRoleService.save(sysRoleDto);

        return JSONResult.ok();
    }

    @SystemLogAnnotation("角色信息跳转修改页面")
    @GetMapping("toUpDate/{id}")
    public String toUpData(@PathVariable Long id, Model model) {
        SysRole sysRole = sysRoleService.getById(id);
        List<SysRole> sysRoleList = sysRoleService.queryAll();
        model.addAttribute("sysRoleList", sysRoleList);
        model.addAttribute("sysRole", sysRole);
        return "sysRole/sysRoleUpDate";
    }

    @SystemLogAnnotation("角色信息修改")
    @PutMapping("update")
    @ResponseBody
    public JSONResult upDate(@RequestBody SysRoleDto sysRoleDto) {

        sysRoleService.update(sysRoleDto);

        return JSONResult.ok();
    }

    @SystemLogAnnotation("角色信息删除")
    @DeleteMapping("delete/{id}")
    @ResponseBody
    public JSONResult delete(@PathVariable Long id) {

        sysRoleService.logicalDelete(id);

        return JSONResult.ok();
    }

    @SystemLogAnnotation("角色信息跳转详情页面")
    @GetMapping("toDetail/{id}")
    public String toDetail(@PathVariable Long id, Model model) {
        SysRole sysRole = sysRoleService.getById(id);
        model.addAttribute("sysRole", sysRole);
        return "sysRole/sysRoleDetail";
    }

    @SystemLogAnnotation("查询所有树资源")
    @GetMapping({"listResource","listResource/{roleId}"})
    @ResponseBody
    public JSONResult listResource(@PathVariable(required = false) Long roleId) {
        List<TreeSysResourceDto> sysResourceVoList = sysResourceService.listSysResource(roleId);
        return JSONResult.ok(sysResourceVoList);
    }
}